<?php
namespace TPH\Controller;
use Admin\Comtroller\AdminController;

Class CommonController extends AdminController
{
    protected $model;   // 当前数据模型
    protected $tableName;   // 当前数据模型对应表面

    public function _initialize()
    {
        parent::_initialize();
        $this->assign('user', session('user_auth'));        // 获取用户session数据
        $this->tableName = $this->tableName ? $this->tableName : CONTROLLER_NAME;
        $this->model = @D($this->tableName);
    }

    /**
     * [index 默认列表页]
     * @return [type] [description]
     */
    public function index()
    {
        $map = $this->_search();
        if (method_exists($this, '_filter')) {
            $this->_filter($map);
        }
        if (!empty($this->model)) {
//            C('LIST_ROWS', 10);    // 修改当前页数量
            $list = $this->lists($this->model, $map);
            $this->assign('list', $list);
        }
        if (method_exists($this, '_befor_index')) {
            $this->_befor_index();
        }
        $this->display();
    }

    /**
     * [_search 搜索/获取查询条件]
     * @return [type] [description]
     */
    protected function _search()
    {
        $map = array();
        foreach ($this->model->getDbFields() as $key => $val) {
            if (isset($_REQUEST['key']) && $_REQUEST['key'] != '') {
                if (in_array($val, C('SEARCHKEY'))) {
                    $map [$val] = array('like', '%' . trim($_REQUEST['key']) . '%');
                }
            }
        }
        $map['_logic'] = 'or';
        if ((IS_POST) && isset($_REQUEST['key']) && $_REQUEST['key'] != '') {
            $where['_complex'] = $map;
            return $where;
        } else {
            return $map;
        }
    }

    /**
     * [add 添加]
     */
    public function add()
    {
        if (IS_POST) {
            $data = $this->model->create();
            if (empty($data)) {
                $this->mtReturn(300, '失败，请检查值是否已经存在', $_REQUEST['navTabId'], true);
            }
            if (method_exists($this, '_befor_insert')) {        // 插入前操作, 过滤数据
                $data = $this->_befor_insert($data);
            }
            if ($id=$this->model->add($data)) {
                if (method_exists($this, '_after_add')) {
                    $this->_after_add($id);         // 插入后操作
                }
                $this->mtReturn(200, "新增成功" . $id, $_REQUEST['navTabId'], true);
            }else{
                $this->mtReturn(300, '添加数据失败', $_REQUEST['navTabId'], true);
            }
        }
        if (method_exists($this, '_befor_add')) {
            $this->_befor_add();
        }
        $this->display();
    }

    /**
     * [edit 编辑/修改]     限制更新单条数据
     * @return [type] [description]
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('request.');
            if (false === $data = $this->model->create($data)) {
                $errorMessahe = $this->model->getError() ? $this->model->getError() : '失败，请检查值是否已经存在';
                $this->mtReturn(300, $errorMessahe, $_REQUEST['navTabId'], true);
            }
            if (method_exists($this, '_befor_update')) {
                $data = $this->_befor_update($data);
            }
            if ($this->model->save($data)) {
                if (method_exists($this, '_after_edit')) {
                    $id = $data['id'];
                    $this->_after_edit($id);
                }
            }
            $this->mtReturn(200, "编辑成功", $_REQUEST['navTabId'], true);
        }
        if (method_exists($this, '_befor_edit')) {
            $this->_befor_edit();
        }
        if($id = I('get.id')){  // 获取当前数据
            $arr = $this->model->find($id);
            $this->assign('arr', $arr);
        }
        $this->display();
    }

    /**
     * [del 删除]
     * @return [type] [description]
     */
    public function del()
    {
        $id = I('get.id');
        if ($id) {
            $data = $this->model->find($id);
            $data['id'] = $id;
            if ($data['status'] == 1) {
                $data['status'] = 0;
                $msg = '锁定';
                if (method_exists($this, '_befor_del')) {
                    $this->_befor_del($id);
                }
            } else {
                $data['status'] = 1;
                $msg = '启用';
            }
            $res = $this->model->save($data);
            if($res == false)
            {
               $this->error('删除失败！');
            }else
            {

            }
            //$this->mtReturn(200, $msg, $_REQUEST['navTabId'], false);
        } else {
            $info = $this->model->where('status=0')->select();
            foreach ($info as $key => $v) {
                $attid = $v['attid'];
                $ad['attid'] = 0;
                M('files')->where(array("attid" => $attid))->save($ad);
            }
            $info = M('files')->where('attid=0 and  uid=' . session('uid'))->select();
            foreach ($info as $key => $v) {
                $filepath = $v['folder'] . $v['filename'];
                unlink($filepath);
            }
            M('files')->where('attid=0 and  uid=' . session('uid'))->delete();
            if (!in_array(session('uid'), C('ADMINISTRATOR'))) {
                $Rs = $this->model->where('status=0 and uid=' . session("uid"))->delete();
            } else {
                $Rs = $this->model->where('status=0')->delete();
            }
            $this->mtReturn(200, '清理' . $Rs . '条无用的记录', $_REQUEST['navTabId'], false);
        }

    }




}